//this js file holds our crew behavoir AI and management functions

var firstNames = Array("Andrew","Brendan", "Shad", "Geoff", "Chinua", "David","Colin", "Michael", "Alexander", "Annie", "Rae", "Michelle", "Burt", "Emily", "Arthur", "Nila", "Alcatraz", "Enoch", "Guy", "Alan", "Malcom", "Yolanda", "Antonio", "John", "Wolfgang","Gertrude", "Lawrence", "Josh", "Henry", "Emilia","Ted", "Nita", "Ender", "Jack");
var lastNames = Array("Rosen", "Bolding", "Benshoof", "Massey", "Umoja", "Weaver", "Wiggens", "Nyugen", "Jha", "Fernandez", "Rossi", "Garcia", "Smith", "Gilson", "Llanes", "Vice", "Reinhold", "Macklin", "Schiff", "Schubert","Lord", "Ryerse", "Robot", "Armstrong", "Croteau", "Arroyo", "Suzuki", "Sato","Endo", "Ito", "Wong", "Li", "Goldberg", "Weingarten", "Singer", "Cantor", "Smirnov", "Ivanov", "Wiggen", "Sparrow");
var shipNames = Array("Titanic", "General Slocum", "Tek Sing", "Sultana", "Bulwark", "Eastland", "Estonia", "Vanguard", "Ramdas", "Captain", "Atlantic", "Grandcamp", "Shamia", "Ramdas", "Novorossiysk", "Bukoba", "Toya Maru", "Kiangya");

//this array is for crew that should be generated in order and only once
//var baseCrewRoles = Array("Captain", "Navigator","Pilot", "Engineer", "Medical Officer", "Head of Security", "Cook");
var baseCrewRoles = Array("Captain", "Navigator","Pilot", "Engineer", "Medic", "Security", "Cook");

//this array is for crew members above te base
//var extraCrewRoles = Array("Navigator", "Engineer", "Medical Officer", "Security Officer", "Cook", "Janitor", "Passenger", "Passenger", "Passenger");
var extraCrewRoles = Array("Navigator", "Engineer", "Medic", "Security", "Cook", "Janitor", "Passenger", "Passenger", "Passenger");



function generateCrewName() 
{
	var first =  firstNames[Math.floor(Math.random()*firstNames.length)];
	var last =  lastNames[Math.floor(Math.random()*lastNames.length)];
	return first + " " +last;
	
}

function generateShipName() 
{
	return shipNames[Math.floor(Math.random()*shipNames.length)];	
}






function make_crew( nothal, size )
{
	if( 'undefined' == typeof size ) { size = nothal.crewsize; }
	//alert("make_crew "+size);
	var i = 0;
	var crew = [];
	var c;
	
	for(i=0;i<size && i< baseCrewRoles.length;i++)
	{
		//alert(""+i+": "+baseCrewRoles[i]);
		c = make_new_crewmember( baseCrewRoles[i], nothal );
		crew.push( c );
		//alert(c.roleid+" "+c.name+" reporting!");
		//console.log(c.roleid+" "+c.name+" reporting!");
	}
	
	for(;i<size;i++)
	{
		//alert(""+i+": "+extraCrewRoles[i]);
		c = make_new_crewmember(extraCrewRoles[Math.floor(Math.random()*extraCrewRoles.length)], nothal);
		crew.push( c );
		//alert(c.roleid+" "+c.name+" reporting!");
		//console.log(c.roleid+" "+c.name+" reporting!");
	}
	
	//alert(crew.length);
	return crew;
}


function make_new_crewmember(role,nothal)
{
	//alert( "make_crew_member "+nothal.crew.length );
	c = new Object();
	c.name = generateCrewName();
	c.x = Math.random()*100;
	c.y = Math.random()*100;
	var w = 100/nothal.width;
	var h = 100/nothal.height;
	var loc = [(Math.floor(c.x/w)+0.5)*(w),(Math.floor(c.y/h)+0.5)*(h)];
	c.x = loc[0];
	c.y = loc[1];
	c.health = 100;
	c.behavoir = [function(){wander(c,nothal.canvas);}];//add per role logic here
	c.canvas = nothal.canvas;
	c.color = [Math.floor(Math.random()*256),Math.floor(Math.random()*256),Math.floor(Math.random()*256)];
	c.roleid = role;
	c.radius = 1;
	c.waypoint = null;//holds the loc of the next room I am walking to
	c.destiation = null;//holds my final destination
	c.path = []//holds by stack of instructions
	//nothal.crew.push(c);
	//alert( "make_crew_member added "+nothal.crew.length )
	return c;
}




function jitter(c,nothal)
{
	
	var r = c.radius+Math.random()*2*c.radius;
	var theta = Math.random()*2*Math.PI;

	deltax = r*Math.cos(theta);
	deltay = r*Math.sin(theta);


	newx = c.x+deltax;
	newy = c.y+deltay;

	if(newx<0)
	{
		newx=0;
	}
	if(newx>nothal.canvas.width)
	{
		newx = nothal.canvas.width
	}
	if(newy<0)
	{
		newy=0;
	}
	if(newy>nothal.canvas.height)
	{
		newy = nothal.canvas.height
	}
	if(is_passable(nothal,newx,newy))
	{
		//console.log(c.name+"+("+deltax+","+deltay+")");
		c.x = newx;
		c.y = newy;
	}	
}


function improved_wander(c,nothal)
{
//alert("walking to room");
	var dest = c.waypoint;
	var roomx,roomy =0;
	var myroom = get_my_room(c.x,c.y,nothal.rooms);
	var room_ref = nothal.rooms[myroom[0]][myroom[1]];
	
	if(c.destiation==null)
	{
		roomx = Math.floor(Math.random()*5);
		roomy = Math.floor(Math.random()*5);
		c.destiation = [roomx,roomy];
		c.way_room = advise_crewman(c,room_ref,nothal.rooms);
		c.waypoint = get_room_center(nothal.rooms,c.way_room.x,c.way_room.y);
	}
	else
	{
		if(c.way_room==null)
		{	
			c.way_room = advise_crewman(c,room_ref,nothal.rooms);
		}
		c.waypoint = get_room_center(nothal.rooms,c.way_room.x,c.way_room.y);
	}
	var c_dest =  get_room_center(nothal.rooms,c.destiation[0],c.destiation[1])
	if(get_dist(c.x,c.y,c_dest[0],c_dest[1])<2.0)
	{
		c.path = [];
		roomx = Math.floor(Math.random()*5);
		roomy = Math.floor(Math.random()*5);
		c.destiation = [roomx,roomy];		
	}
	if(get_dist(c.x,c.y,c.waypoint[0],c.waypoint[1])<2.0)
	{
		c.way_room = advise_crewman(c,room_ref,nothal.rooms);
		try
		{
		c.waypoint = get_room_center(nothal.rooms,way_room.x,way_room.y);
		}
		catch(err)
		{;}
	}	
	
	var dest = c.waypoint;

	var r = c.radius+Math.random()*2*c.radius;
	

	var deltax = c.x-dest[0];
	var deltay = c.y-dest[1];

	var mag = Math.sqrt(deltax*deltax+deltay*deltay);
	deltax = (deltax/mag)*r;
	deltay = (deltay/mag)*r;
	//alert([deltax,deltay]);

	newx = c.x-deltax;
	newy = c.y-deltay;

	if(newx<0)
	{
		newx=0;
	}
	if(newx>nothal.canvas.width)
	{
		newx = nothal.canvas.width
	}
	if(newy<0)
	{
		newy=0;
	}
	if(newy>nothal.canvas.height)
	{
		newy = nothal.canvas.height
	}

	if(is_passable(nothal,newx,newy))
	{
		c.x = newx;
		c.y = newy;
	}	

	
}


function walk_to_room(c,roomx,roomy,nothal)
{
//alert("walking to room");
	var dest = c.waypoint;
	if(c.waypoint==null||get_dist(c.x,c.y,c.waypoint[0],c.waypoint[1])<1.0)
	{
		dest = get_next_waypoint(c.x,c.y,roomx,roomy,canvas);	
		c.waypoint = dest;
	}
	
//alert(dest);

	var r = c.radius+Math.random()*2*c.radius;
	

	var deltax = c.x-dest[0];
	var deltay = c.y-dest[1];

	var mag = Math.sqrt(deltax*deltax+deltay*deltay);
	deltax = (deltax/mag)*r;
	deltay = (deltay/mag)*r;
	//alert([deltax,deltay]);

	newx = c.x-deltax;
	newy = c.y-deltay;

	if(newx<0)
	{
		newx=0;
	}
	if(newx>canvas.width)
	{
		newx = canvas.width
	}
	if(newy<0)
	{
		newy=0;
	}
	if(newy>canvas.height)
	{
		newy = canvas.height
	}

	if(is_passable(nothal,newx,newy))
	{
		c.x = newx;
		c.y = newy;
	}	

	
}



function crew_acts(nothal)
{

	for(var i=0;i<nothal.crew.length;i++)
	{
		var c = nothal.crew[i];
		improved_wander(c,nothal);
	}

}

function render_crew(nothal)
{
	var context = nothal.canvas.getContext('2d');
	var radius = 1.5*(nothal.canvas.height+nothal.canvas.width)/200
	for(var i=0;i<nothal.crew.length;i++)
	{
		//////alert("hi"+nothal.crew.length);


		var c = nothal.crew[i];
		var hoffset = -2;
		var voffset = -2.5;
		var role = c.roleid.toLowerCase()
		//alert(role);
		var sprite = images[role];
		//console.log(role);
		

		context.fillStyle = "white";
    	context.font = "10pt Helvetica";
    	context.fillText(c.name, scale(c.x+voffset,nothal.canvas.width), scale(c.y+hoffset,nothal.canvas.height));
		context.beginPath();
		context.arc(scale(c.x,nothal.canvas.width), scale(c.y,nothal.canvas.height), c.radius*radius, 0, 2 * Math.PI, false);
		context.fillStyle = "rgb("+c.color+")";
		context.fill();
		context.stroke();
		//alert(sprite);
		if(role!="passenger")
		{
			draw_sprite(sprite,nothal.canvas,c.x-radius*c.radius/10,c.y-radius*c.radius/10,radius*c.radius/5,radius*c.radius/5)
		}
	}	
}
